package com.sisSaude.daos;

import java.sql.ResultSet;
import java.util.Vector;

import com.sisSaude.entityBeans.ItemDeControleDeFrequenciaCardiaca;
import com.sisSaude.utils.DataProvider;
import com.sisSaude.utils.SqlFactory;

public class ItemDeControleDeFrequenciaCardiacaDAO extends GenericDAO<ItemDeControleDeFrequenciaCardiaca> {

	// fields
	public static String SELECT_BASICA = " SELECT ItemDeControleDeFrequenciaCardiaca.* " +
			                             " FROM ItemDeControleDeFrequenciaCardiaca "; 
	
	// public methods
	public boolean delete(ItemDeControleDeFrequenciaCardiaca item) {
		String sql = SqlFactory.getSqlDelete("ItemDeControleDeFrequenciaCardiaca", "id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(item.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
	public ItemDeControleDeFrequenciaCardiaca get(int id) {
		ItemDeControleDeFrequenciaCardiaca item = null;
		String sql = SELECT_BASICA + " WHERE ItemDeControleDeFrequenciaCardiaca.id = ? "; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(id);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				item = new ItemDeControleDeFrequenciaCardiaca();
				loadAttribs(item, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return item;
	}
	public void loadAttribs(ItemDeControleDeFrequenciaCardiaca item, ResultSet r) {
		try {
			item.setId(r.getInt("id"));
			item.setIdControle(r.getInt("idControle"));
			item.setDuracao(r.getInt("duracao"));
			item.setVelocidade(r.getDouble("velocidade"));
			item.setBpm(r.getInt("bpm"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// protected methods
	protected boolean insert(ItemDeControleDeFrequenciaCardiaca item) {
		String sql = SqlFactory.getSqlInsert("ItemDeControleDeFrequenciaCardiaca");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(item.getId());
		params.add(item.getIdControle());
	 	params.add(item.getDuracao());
	 	params.add(item.getVelocidade());
	 	params.add(item.getBpm());
	 	try {
			item.setId(dataProvider.insert(sql, params));
			return item.getId() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
	}
	protected boolean update(ItemDeControleDeFrequenciaCardiaca item) {
		String sql = SqlFactory.getSqlUpdate("ItemDeControleDeFrequenciaCardiaca", "ItemDeControleDeFrequenciaCardiaca.id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(item.getIdControle());
	 	params.add(item.getDuracao());
	 	params.add(item.getVelocidade());
	 	params.add(item.getBpm());
		params.add(item.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
}
